package com.compass.doctor.ui.patient.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.base.BaseVBFragment
import com.compass.doctor.databinding.FragmentBaseListBinding
import com.compass.doctor.ui.mine.adapter.InquiryRecordAdapter
import com.compass.doctor.ui.patient.adapter.InquiryBriefAdapter
import com.compass.doctor.ui.patient.viewmodel.PatientViewModel
import com.compass.framework.constant.KEY_PATIENT_ID
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.ext.gone
import com.compass.framework.ext.visible
import com.compass.framework.utils.dpToPx
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * Description： 问诊小结
 * Date:2023/12/8 10:42
 */
class InquiryBriefFragment : BaseVBFragment<FragmentBaseListBinding>(), OnRefreshListener,
    OnLoadMoreListener {

    private lateinit var mAdapter: InquiryBriefAdapter
    private val patientViewModel: PatientViewModel by viewModels()
    private var patientId: String? = null //患者id
    private var mPage = 1
    override fun initView(view: View, savedInstanceState: Bundle?) {
        arguments?.let {
            patientId = it.getString(KEY_PATIENT_ID)
        }
        mAdapter = InquiryBriefAdapter()
        mBinding?.refreshLayout?.apply {
            setEnableRefresh(true)
            setEnableLoadMore(false)
            setOnRefreshListener(this@InquiryBriefFragment)
            setOnLoadMoreListener(this@InquiryBriefFragment)
        }

        mBinding?.recyclerView?.apply {
            layoutManager =
                LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
            adapter = mAdapter
            addItemDecoration(NormalItemDecoration().apply {
                setBounds(
                    left = dpToPx(8),
                    top = dpToPx(10),
                    right = dpToPx(8),
                    bottom = dpToPx(10)
                )
                setLastBottom(true)
            })
        }
    }

    override fun initData() {
        showLoading()
        patientViewModel.recordLiveData.observe(this){
            mAdapter.setData(it)
            mBinding?.refreshLayout?.finishRefresh()
            if (it.isNullOrEmpty()) {
                mBinding?.emptyView?.visible()
            } else {
                mBinding?.emptyView?.gone()
            }

            dismissLoading()
        }

        getConsultationList()
    }

    private fun getConsultationList() {
        patientViewModel.getBriefSummary(patientId )
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        getConsultationList()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {

    }
}